package com.parsechina.hpush.broker.parser;

import io.netty.handler.codec.mqtt.*;

/**
 * @author linfeng
 * @since 2016/11/22
 */
public class PubAckMessage extends PushMessage {
    private static final long serialVersionUID = 3000829421224468921L;

    private int messageId;

    public void setMessageId(int messageId) {
        this.messageId = messageId;
    }

    private MqttPubAckMessage pubAckMessage;

    public PubAckMessage(MqttPubAckMessage msg) {
        super(msg);
        this.pubAckMessage = msg;
    }

    public PubAckMessage() {
    }

    public int getMessageId() {
        if (pubAckMessage == null) {
            return messageId;
        }
        return pubAckMessage.variableHeader().messageId();
    }

    public MqttPubAckMessage build() {
        MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBACK, false, MqttQoS.AT_MOST_ONCE, false, 0);
        MqttMessageIdVariableHeader variableHeader = MqttMessageIdVariableHeader.from(messageId);
        return new MqttPubAckMessage(fixedHeader, variableHeader);
    }

}
